package com.tiny.tinyweb.core.ldap.entry;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public final class Person extends MapEntry {

    private static final long serialVersionUID = 1L;

    @Override
    public List<String> getDnKeys() {
        return Arrays.asList("cn", "ou");
    }

    public Person() {
        this.init();
    }

    public Person(String cn, List<String> ous) {
        this.init();
        this.setCn(cn);
        for (String ou : ous) {
            this.addOu(ou);
        }
    }

    public Person(String cn,
            String sn,
            List<String> ous,
            List<String> mails,
            byte[] userPassword) {
        this.init();
        this.setCn(cn);
        this.setSn(sn);
        for (String ou : ous) {
            this.addOu(ou);
        }
        for (String mail : mails) {
            this.addMail(mail);
        }
        this.setUserPassword(userPassword);
    }

    public Person(Person person) {
        this.init();
        this.setCn(person.getCn());
        this.setSn(person.getSn());
        for (String ou : person.getOu()) {
            this.addOu(ou);
        }
        for (String mail : person.getMail()) {
            this.addMail(mail);
        }
        this.setUserPassword(person.getUserPassword());
    }

    private void init() {
        this.put("objectClass", Arrays.asList(
                "inetOrgPerson", "organizationalPerson", "person", "top"));
        this.put("cn", null);
        this.put("sn", null);
        this.put("ou", new ArrayList<String>());
        this.put("mail", new ArrayList<String>());
        this.put("userPassword", null);
    }

    @SuppressWarnings("unchecked")
    public List<String> getObjectClass() {
        return Collections.unmodifiableList(
                (List<String>) this.get("objectClass"));
    }

    public String getCn() {
        return (String) this.get("cn");
    }

    public void setCn(String cn) {
        this.put("cn", cn);
    }

    public String getSn() {
        return (String) this.get("sn");
    }

    public void setSn(String sn) {
        this.put("sn", sn);
    }

    @SuppressWarnings("unchecked")
    public void addOu(String ou) {
        ((List<String>) this.get("ou")).add(ou);
    }

    @SuppressWarnings("unchecked")
    public void removeOu(String ou) {
        ((List<String>) this.get("ou")).remove(ou);
    }

    @SuppressWarnings("unchecked")
    public List<String> getOu() {
        return (List<String>) this.get("ou");
    }

    @SuppressWarnings("unchecked")
    public void addMail(String mail) {
        ((List<String>) this.get("mail")).add(mail);
    }

    @SuppressWarnings("unchecked")
    public void removeMail(String mail) {
        ((List<String>) this.get("mail")).remove(mail);
    }

    @SuppressWarnings("unchecked")
    public List<String> getMail() {
        return (List<String>) this.get("mail");
    }

    public byte[] getUserPassword() {
        return (byte[]) this.get("userPassword");
    }

    public void setUserPassword(byte[] userPassword) {
        this.put("userPassword", userPassword);
    }
}
